<template>
  <div class="main app-container">
    <div class="main-container">
      <div class="cbox">
        <el-form class="form" :model="flt" label-width="6rem" ref="fltForm">
          <el-row :gutter="10" class="row">
            <el-col :span="4">
              <el-form-item size="mini" label="省：">
                <el-select clearable v-model="flt.tmnPrvoCode" style="width: 100%;"
                  @change="changeProvince">
                  <el-option
                    v-for="(item, index) in provinceList"
                    :value="item.PRV_CODE"
                    :label="item.PRV_CHNNAME"
                    :key="index"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="区县：" prop="tmnCtyCode">
                <el-select clearable v-model="flt.tmnCtyCode" style="width: 100%;">
                  <el-option
                    v-for="(item, index) in cityList"
                    :value="item.ctyCode"
                    :label="item.ctyChnname"
                    :key="index"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="5">
              <el-form-item label-width="13rem" size="mini" label="中文名称：" prop="tmnChnname">
                <el-input clearable maxlength="32" v-model="flt.tmnChnname" size="mini" style="width: 100%"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="2">
              <el-form-item size="mini" label-width="10px">
                <el-button size="mini" type="primary" @click="searchList">查询</el-button>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10" class="row">
            <el-col :span="24">
              <el-form-item size="mini" label-width="0px">
                <el-button size="mini" type="primary" plain @click="addNew">新增</el-button>
                <el-button size="mini" type="primary" plain @click="edit">编辑</el-button>
                <el-button size="mini" type="primary" plain @click="del">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="table">
        <div class="inner" ref="inner">
          <el-table :height="maxHeight" style="width: 100%" :data="terminalyardList" :highlight-current-row="true" @row-click="rowClick" border :header-cell-style="{
              textAlign: 'center'
            }"
            @row-dblclick="view">
            <el-table-column align="center" width="60" label="序号" :index="indexMethod" type="index"></el-table-column>
            <el-table-column align="left" show-overflow-tooltip label="国家" prop="tmnCoutryName"></el-table-column>
            <el-table-column align="left" show-overflow-tooltip label="省" prop="tmnPrvoCodeName"></el-table-column>
            <el-table-column align="left" show-overflow-tooltip label="市" prop="tmnCtyeCodeName"></el-table-column>
            <el-table-column align="left" show-overflow-tooltip label="区县" prop="tmnCtyCodeName"></el-table-column>
            <el-table-column align="left" show-overflow-tooltip label="中文名称" prop="tmnChnname"></el-table-column>
            <el-table-column align="left" show-overflow-tooltip label="英文名称" prop="tmnEgsname"></el-table-column>
            <el-table-column align="center" show-overflow-tooltip label="简称中文" prop="tmnShrchn"></el-table-column>
            <el-table-column align="left" show-overflow-tooltip label="地址" prop="tmnAddress"></el-table-column>
            <el-table-column align="center" show-overflow-tooltip label="码头堆场标志" prop="tmnTyflagName"></el-table-column>
            <el-table-column align="left" show-overflow-tooltip label="创建员工" prop="tmnInsertuser"></el-table-column>
            <el-table-column align="left" show-overflow-tooltip label="创建时间" prop="tmnInsertdt"></el-table-column>
            <el-table-column align="left" show-overflow-tooltip label="最后修改员工" prop="tmnLstupduser"></el-table-column>
            <el-table-column align="left" show-overflow-tooltip label="最后修改时间" prop="tmnLstupddt"></el-table-column>
          </el-table>
        </div>
      </div>
      <el-pagination
        class="pagination"
        small
        background
        ref="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="Pagination.currentPage"
        :page-sizes="Pagination.pageSizeList"
        :page-size="Pagination.pageSize"
        :layout="Pagination.layout"
        :total="Pagination.total">
      </el-pagination>
      <el-dialog
        v-el-drag-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="showDialog"
        :visible.sync="showDialog"
        :title="operate">
        <v-dialog
          :disabled="!isEditMode"
          @close="closeEdit"
          @save="saveFltInfo"
          :flt_info="currentFltInfo">
        </v-dialog>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import VDialog from './dialog'
import resizeMixin from '@/mixins/resize'
import paginationMixin from '@/mixins/pagination'

const DIALOG_MODE = {
  ADD: '新增',
  EDIT: '编辑',
  VIEW: '详情'
}

import {
  getYardService,
  addYardService,
  editYardService,

  getProvinceService,
  getAreaService,

  delYardService
} from '@/request/api'

export default {
  name: 'router.terminalYard',
  mixins: [resizeMixin, paginationMixin],
  data() {
    return {
      province: '',
      provinceList: [],
      cityList: [],
      flt: {
        tmnPrvoCode: '',
        tmnCtyCode: '',
        tmnChnname: ''
      },
      terminalyardList: [],
      TY_FLAG: {},

      isEditMode: true,
      operate: '',
      showDialog: false,

      currentRow: {},
      currentFltInfo: {},

      roleStatus: [],

      maxHeight: -1
    }
  },
  computed: {
  },
  methods: {
    // 搜索
    searchList() {
      this.getList()
    },
    // 重写的获取列表数据的方法
    getList() {
      this.clearState()
      let needArr = Object.assign(this.flt,{PageIndexs: this.Pagination.currentPage,PageSizes: this.Pagination.pageSize})
      getYardService(needArr).then(res => {
        this.terminalyardList = res.data.FTKJWLHYPT
        this.Pagination.total = res.data.total.FTKJWLHYPT
      })
    },
    addNew() {
      this.operate = DIALOG_MODE.ADD
      this.isEditMode = true
      this.currentFltInfo = {

      }
      this.showDialog = true
    },
    edit() {
      if (Object.keys(this.currentRow).length == 0) {
        this.$alert('请选择需要编辑的数据！', '提示', {
          confirmButtonText: '确定'
        })
        return
      }
      console.log(this.currentRow)
      this.operate = DIALOG_MODE.EDIT
      this.isEditMode = true
      this.currentFltInfo = Object.assign({}, this.currentRow)
      this.showDialog = true
    },
    view (row) {
      this.operate = DIALOG_MODE.VIEW
      this.isEditMode = false
      this.currentFltInfo = Object.assign({}, row)
      this.showDialog = true
    },

    closeEdit() { this.showDialog = false },

    saveFltInfo(info) {
      switch(this.operate) {
      case DIALOG_MODE.ADD:
        addYardService(info).then(() => {
          this.$message({
            message: '新增成功！',
            type: 'success',
            showClose: true
          })
          this.showDialog = false
          this.getList()
        })
        break
      case DIALOG_MODE.EDIT:
        editYardService(info).then(() => {
          this.$message({
            message: '编辑成功！',
            type: 'success',
            showClose: true
          })
          this.showDialog = false
          this.getList()
        })
        break
      default:
        break
      }
    },
    rowClick(row) { this.currentRow = row },
    del() {
      if (Object.keys(this.currentRow).length == 0) {
        this.$alert('请选择需要删除的数据', '提示', {
          confirmButtonText: '确定'
        })
        return
      }
      this.$confirm('您确认要删除选定的数据吗?', '提示', {
        closeOnClickModal: false
      }).then(() => {
        delYardService({tmnCode: this.currentRow.tmnCode}).then(() => {
          this.$message({
            message: '删除成功！',
            type: 'success',
            showClose: true
          })
          this.getList()
        })
      })
    },
    clearState() {
      this.currentRow = {}
      this.currentFltInfo = {}
    },
    changeProvince (uuid) {
      this.flt.tmnCtyCode = ''
      this.getCities(uuid)
    },
    getProvinces () {
      getProvinceService({ctycd: ''}).then(res => {
        this.provinceList = res.data
      })
    },
    getCities (UUID) {
      getAreaService({
        prvCode: UUID,
        ctyeCode: ''
      }).then(res => {
        this.cityList = res.data.FTKJWLHYPT
      })
    },
  },
  components: {
    VDialog
  },
  mounted() {
    this.getList()
    this.getProvinces()
  },
  created() {
    this.resize = () => {
      this.maxHeight = this.$refs.inner.offsetHeight
    }
  }
}
</script>

<style lang="scss" scoped>
.main {
  height: 100%;
  position: relative;
  width: 100%;
}
.main-container {
  height: 100%;
  width: 100%;
  position: relative;
  // padding: 0 12px;
  display: flex;
  flex-direction: column;
  .form {
    width: 100%;
    flex: 0 0 auto;
    .row {
      width: 100%;
      // margin: 10px 0;
      margin-bottom: 0;
    }
  }
  .table {
    flex: 1;
    overflow: hidden;
    position: relative;
    .inner {
      width: 100%;
      height: 100%;
      overflow: hidden;
    }
  }
}
</style>
